﻿Option Explicit On
Option Strict On

Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels

Imports ACOMDirector
Imports ACOMDirector.Framework.Hardware
Imports ACOMDirector.Framework.Hardware.Remoting
Imports ACOMDirector.Configuration

Public Class ControlPanelLarge

#Region " Private Members "

    Private m_StateManager As StateManagerRemotableWrapper

    'Auto Determine Operate Mode
    Private m_DisableEvents As Boolean = False
    Private m_DoNotRefresh As Boolean = False

    'Matrix Display Line Indexes
    Private m_DisplayLine1Index As Integer
    Private m_DisplayLine2Index As Integer
    Private m_DisplayLine3Index As Integer
    Private m_DisplayLine4Index As Integer

    Private m_CurrentLocation As Drawing.Point

#End Region

#Region " Form Events "

    Private Sub MainForm_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        If m_StateManager IsNot Nothing Then
            RemoveHandler m_StateManager.StateChanged, AddressOf StateChangedHandler
            RemoveHandler m_StateManager.ForwardPowerLedChanged, AddressOf ForwardPowerLedChangedHandler
            RemoveHandler m_StateManager.ReflectedPowerLedChanged, AddressOf ReflectedPowerLedChangedHandler

            m_StateManager = Nothing
        End If

        'Save the Window Location
        SystemConfiguration.Instance.ACOMConfiguration.LargeControlPanelLocationX = m_CurrentLocation.X
        SystemConfiguration.Instance.ACOMConfiguration.LargeControlPanelLocationY = m_CurrentLocation.Y
        SystemConfiguration.Instance.ACOMConfiguration.Save()

    End Sub

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Attach the State Manager object
        m_StateManager = Session.GetSession.StateManager
        AddHandler m_StateManager.StateChanged, AddressOf StateChangedHandler
        AddHandler m_StateManager.ForwardPowerLedChanged, AddressOf ForwardPowerLedChangedHandler
        AddHandler m_StateManager.ReflectedPowerLedChanged, AddressOf ReflectedPowerLedChangedHandler

        'Setup the Matrix Display
        MatrixControl.SetLedStyle(LedStyle.Square)
        MatrixControl.LoadFontCollectionFromResource("ACOMDirector.LedMatrixSymbolFile.xml")

        m_DisplayLine1Index = MatrixControl.AddTextItem(m_StateManager.State.DisplayLine1, New Point(0, 0), ItemDirection.Right, ItemSpeed.Idle)
        m_DisplayLine2Index = MatrixControl.AddTextItem(m_StateManager.State.DisplayLine2, New Point(0, 8), ItemDirection.Right, ItemSpeed.Idle)
        m_DisplayLine3Index = MatrixControl.AddTextItem(m_StateManager.State.DisplayLine3, New Point(0, 16), ItemDirection.Right, ItemSpeed.Idle)
        m_DisplayLine4Index = MatrixControl.AddTextItem(m_StateManager.State.DisplayLine4, New Point(0, 24), ItemDirection.Right, ItemSpeed.Idle)

        'Populate the Measurement Metrics
        Dim DTMetricsA As New DataTable
        DTMetricsA.Columns.Add("Name", GetType(String))
        DTMetricsA.Columns.Add("Value", GetType(Integer))
        DTMetricsA.Rows.Add("Bias", Framework.Hardware.MeasurementMetricType.Bias)
        DTMetricsA.Rows.Add("DC Input", Framework.Hardware.MeasurementMetricType.DCInput)
        DTMetricsA.Rows.Add("Drive", Framework.Hardware.MeasurementMetricType.Drive)
        DTMetricsA.Rows.Add("Frequency", Framework.Hardware.MeasurementMetricType.Frequency)
        DTMetricsA.Rows.Add("Forward Power", Framework.Hardware.MeasurementMetricType.ForwardPower)
        DTMetricsA.Rows.Add("Filament Voltage", Framework.Hardware.MeasurementMetricType.FilamentVoltage)
        DTMetricsA.Rows.Add("Power Gain", Framework.Hardware.MeasurementMetricType.PowerGain)
        DTMetricsA.Rows.Add("HV", Framework.Hardware.MeasurementMetricType.HV)
        DTMetricsA.Rows.Add("IF Front Tube", Framework.Hardware.MeasurementMetricType.IFFrontTube)
        DTMetricsA.Rows.Add("IF Rear Tube", Framework.Hardware.MeasurementMetricType.IFRearTube)
        DTMetricsA.Rows.Add("Grid Current", Framework.Hardware.MeasurementMetricType.GridCurrent)
        DTMetricsA.Rows.Add("Plate Current", Framework.Hardware.MeasurementMetricType.PlateCurrent)
        DTMetricsA.Rows.Add("Screen Current", Framework.Hardware.MeasurementMetricType.ScreenCurrent)
        DTMetricsA.Rows.Add("Mains Voltage", Framework.Hardware.MeasurementMetricType.MainsVoltage)
        DTMetricsA.Rows.Add("Plate RF Voltage", Framework.Hardware.MeasurementMetricType.PlateRFVoltage)
        DTMetricsA.Rows.Add("Reflected Power", Framework.Hardware.MeasurementMetricType.ReflectedPower)
        DTMetricsA.Rows.Add("Screen Voltage", Framework.Hardware.MeasurementMetricType.ScreenVoltage)
        DTMetricsA.Rows.Add("Front Tube Temp", Framework.Hardware.MeasurementMetricType.FrontTubeTemp)
        DTMetricsA.Rows.Add("Rear Tube Temp", Framework.Hardware.MeasurementMetricType.RearTubeTemp)
        DTMetricsA.Rows.Add("VSWR", Framework.Hardware.MeasurementMetricType.VSWR)

        Dim DTMetricsB As New DataTable
        DTMetricsB.Columns.Add("Name", GetType(String))
        DTMetricsB.Columns.Add("Value", GetType(Integer))
        DTMetricsB.Rows.Add("Bias", Framework.Hardware.MeasurementMetricType.Bias)
        DTMetricsB.Rows.Add("DC Input", Framework.Hardware.MeasurementMetricType.DCInput)
        DTMetricsB.Rows.Add("Drive", Framework.Hardware.MeasurementMetricType.Drive)
        DTMetricsB.Rows.Add("Frequency", Framework.Hardware.MeasurementMetricType.Frequency)
        DTMetricsB.Rows.Add("Forward Power", Framework.Hardware.MeasurementMetricType.ForwardPower)
        DTMetricsB.Rows.Add("Filament Voltage", Framework.Hardware.MeasurementMetricType.FilamentVoltage)
        DTMetricsB.Rows.Add("Power Gain", Framework.Hardware.MeasurementMetricType.PowerGain)
        DTMetricsB.Rows.Add("HV", Framework.Hardware.MeasurementMetricType.HV)
        DTMetricsB.Rows.Add("IF Front Tube", Framework.Hardware.MeasurementMetricType.IFFrontTube)
        DTMetricsB.Rows.Add("IF Rear Tube", Framework.Hardware.MeasurementMetricType.IFRearTube)
        DTMetricsB.Rows.Add("Grid Current", Framework.Hardware.MeasurementMetricType.GridCurrent)
        DTMetricsB.Rows.Add("Plate Current", Framework.Hardware.MeasurementMetricType.PlateCurrent)
        DTMetricsB.Rows.Add("Screen Current", Framework.Hardware.MeasurementMetricType.ScreenCurrent)
        DTMetricsB.Rows.Add("Mains Voltage", Framework.Hardware.MeasurementMetricType.MainsVoltage)
        DTMetricsB.Rows.Add("Plate RF Voltage", Framework.Hardware.MeasurementMetricType.PlateRFVoltage)
        DTMetricsB.Rows.Add("Reflected Power", Framework.Hardware.MeasurementMetricType.ReflectedPower)
        DTMetricsB.Rows.Add("Screen Voltage", Framework.Hardware.MeasurementMetricType.ScreenVoltage)
        DTMetricsB.Rows.Add("Front Tube Temp", Framework.Hardware.MeasurementMetricType.FrontTubeTemp)
        DTMetricsB.Rows.Add("Rear Tube Temp", Framework.Hardware.MeasurementMetricType.RearTubeTemp)
        DTMetricsB.Rows.Add("VSWR", Framework.Hardware.MeasurementMetricType.VSWR)

        m_DisableEvents = True
        cboMetricA.BeginUpdate()
        cboMetricA.DataSource = DTMetricsA
        cboMetricA.ValueMember = "Value"
        cboMetricA.DisplayMember = "Name"
        cboMetricA.EndUpdate()

        cboMetricB.BeginUpdate()
        cboMetricB.DataSource = DTMetricsB
        cboMetricB.ValueMember = "Value"
        cboMetricB.DisplayMember = "Name"
        cboMetricB.EndUpdate()

        cboMetricA.SelectedValue = m_StateManager.State.MetricA
        cboMetricB.SelectedValue = m_StateManager.State.MetricB

        m_DisableEvents = False
        RefreshStatus()

    End Sub

    Private Sub MainForm_Move(sender As Object, e As System.EventArgs) Handles Me.Move
        If Not Me.WindowState = FormWindowState.Minimized Then
            m_CurrentLocation = New Drawing.Point(Me.Location.X, Me.Location.Y)
        End If
    End Sub

#End Region

#Region " Private Methods "

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Delegate Sub RefreshStatusDelegate()
    Private Sub RefreshStatus()
        If m_DoNotRefresh Then Exit Sub

        If Me.InvokeRequired Then
            ' if operating on a thread, invoke a delegate
            ' on the UI thread.
            Dim omd As RefreshStatusDelegate = _
            New RefreshStatusDelegate(AddressOf RefreshStatus)
            Dim arx As IAsyncResult = Me.BeginInvoke( _
              omd, New Object() {})
            Me.EndInvoke(arx)
            Return
        End If

        m_DisableEvents = True

        If Not m_StateManager.State.CanConnect Then
            cmdConnect.Enabled = False
        ElseIf Not m_StateManager.COMPortOpen Then
            cmdConnect.Enabled = True
        End If

        If Not cboMetricA.DroppedDown Then
            cboMetricA.SelectedValue = m_StateManager.State.MetricA
        End If

        If Not cboMetricB.DroppedDown Then
            cboMetricB.SelectedValue = m_StateManager.State.MetricB
        End If

        cmdAntenna0.Enabled = m_StateManager.UIState.Antenna0Enabled
        cmdAntenna1.Enabled = m_StateManager.UIState.Antenna1Enabled
        cmdAntenna2.Enabled = m_StateManager.UIState.Antenna2Enabled
        cmdAntenna3.Enabled = m_StateManager.UIState.Antenna3Enabled
        cmdAntenna4.Enabled = m_StateManager.UIState.Antenna4Enabled
        cmdAntenna5.Enabled = m_StateManager.UIState.Antenna5Enabled
        cmdAntenna6.Enabled = m_StateManager.UIState.Antenna6Enabled
        cmdAntenna7.Enabled = m_StateManager.UIState.Antenna7Enabled
        cmdAntenna8.Enabled = m_StateManager.UIState.Antenna8Enabled
        cmdAntenna9.Enabled = m_StateManager.UIState.Antenna9Enabled
        cmdConnect.Enabled = m_StateManager.UIState.ConnectEnabled
        cmdDisconnect.Enabled = m_StateManager.UIState.DisconnectEnabled
        cboMetricA.Enabled = m_StateManager.UIState.MetricASelectionEnabled
        cboMetricB.Enabled = m_StateManager.UIState.MetricBSelectionEnabled
        cmdOnOff.Enabled = m_StateManager.UIState.OnOffEnabled
        cmdOperateStandby.Enabled = m_StateManager.UIState.OperateStandbyEnabled
        cmdStartMeasuring.Enabled = m_StateManager.UIState.StartMeasuringEnabled
        cmdStopMeasuring.Enabled = m_StateManager.UIState.StopMeasuringEnabled

        MatrixControl.SetItemText(m_DisplayLine1Index, m_StateManager.State.DisplayLine1)
        MatrixControl.SetItemText(m_DisplayLine2Index, m_StateManager.State.DisplayLine2)
        MatrixControl.SetItemText(m_DisplayLine3Index, m_StateManager.State.DisplayLine3)
        MatrixControl.SetItemText(m_DisplayLine4Index, m_StateManager.State.DisplayLine4)

        ledPower.State = m_StateManager.State.PowerLED
        ledOperate.State = m_StateManager.State.OperateLED
        ledStandby.State = m_StateManager.State.StandbyLED
        ledPlate.State = m_StateManager.State.PlateLED
        ledDrive.State = m_StateManager.State.DriveLED
        ledHeater.State = m_StateManager.State.HeaterLED
        ledProtection.State = m_StateManager.State.ProtectionLED

        m_DisableEvents = False

    End Sub

    Private Sub Connect()
        cmdConnect.Enabled = False
        cmdDisconnect.Enabled = True
        m_StateManager.Connect()
    End Sub

#End Region

#Region " Event Handlers "

    Private Sub ForwardPowerLedChangedHandler(ByVal sender As Object, ByVal e As PowerLedEventArgs)
        meterForwardPower.Level = e.Level
    End Sub

    Private Sub ReflectedPowerLedChangedHandler(ByVal sender As Object, ByVal e As PowerLedEventArgs)
        meterReflectedPower.Level = e.Level
    End Sub

    Private Sub StateChangedHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        RefreshStatus()
    End Sub

    Private Sub cmdOperateStandby_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOperateStandby.Click
        m_StateManager.OperateStandby()
    End Sub

    Private Sub cmdAntenna0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna0.Click
        m_StateManager.SelectAntenna0()
    End Sub

    Private Sub cmdAntenna1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna1.Click
        m_StateManager.SelectAntenna1()
    End Sub

    Private Sub cmdAntenna2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna2.Click
        m_StateManager.SelectAntenna2()
    End Sub

    Private Sub cmdAntenna3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna3.Click
        m_StateManager.SelectAntenna3()
    End Sub

    Private Sub cmdAntenna4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna4.Click
        m_StateManager.SelectAntenna4()
    End Sub

    Private Sub cmdAntenna5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna5.Click
        m_StateManager.SelectAntenna5()
    End Sub

    Private Sub cmdAntenna6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna6.Click
        m_StateManager.SelectAntenna6()
    End Sub

    Private Sub cmdAntenna7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna7.Click
        m_StateManager.SelectAntenna7()
    End Sub

    Private Sub cmdAntenna8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna8.Click
        m_StateManager.SelectAntenna8()
    End Sub

    Private Sub cmdAntenna9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAntenna9.Click
        m_StateManager.SelectAntenna9()
    End Sub

    Private Sub cmdOnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOnOff.Click
        'If the power is already on then turn it off?
        If m_StateManager.State.PowerState = PowerState.Connected Then
            If MsgBox("Are you sure you want to turn off the ACOM2000A?", MsgBoxStyle.YesNo, "Turn Off ACOM2000A") = MsgBoxResult.Yes Then
                m_StateManager.OnOff()
            End If
        ElseIf m_StateManager.State.PowerState = PowerState.PoweredOff Then
            If m_StateManager.SupportsRemotePowerOn Then
                If MsgBox("Would you like to remotely turn on the ACOM2000A?", MsgBoxStyle.YesNo, "Turn On ACOM2000A") = MsgBoxResult.Yes Then
                    m_StateManager.OnOff()
                End If
            End If
        End If
    End Sub

    Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
        m_StateManager.Connect()
    End Sub


    Private Sub cmdDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisconnect.Click
        m_StateManager.Disconnect()
        cmdConnect.Enabled = True
        cmdDisconnect.Enabled = False
        m_StateManager.Disconnect()
    End Sub

    Private Sub cmdStartMeasuring_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStartMeasuring.Click

        Dim MetricA As MeasurementMetricType = DirectCast(cboMetricA.SelectedValue, MeasurementMetricType)
        Dim MetricB As MeasurementMetricType = DirectCast(cboMetricB.SelectedValue, MeasurementMetricType)

        m_StateManager.StartMeasuring(MetricA, MetricB)

    End Sub

    Private Sub cmdStopMeasuring_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStopMeasuring.Click
        m_StateManager.StopMeasuring()
    End Sub

    Private Sub cboMetricB_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboMetricB.SelectedValueChanged
        If m_DisableEvents Then Exit Sub
        If IsNumeric(cboMetricB.SelectedValue) Then
            Dim MetricB As MeasurementMetricType = DirectCast(cboMetricB.SelectedValue, MeasurementMetricType)
            m_StateManager.SetMetricB(MetricB)
        End If
    End Sub

    Private Sub cboMetricA_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboMetricA.SelectedValueChanged
        If m_DisableEvents Then Exit Sub
        If IsNumeric(cboMetricA.SelectedValue) Then
            Dim MetricA As MeasurementMetricType = DirectCast(cboMetricA.SelectedValue, MeasurementMetricType)
            m_StateManager.SetMetricA(MetricA)
        End If
    End Sub

#End Region

End Class
